﻿//
// XN4SL - Xna Framework for Silverlight.
//
using System;
using System.Collections.Generic;

namespace Xn4Sl.Framework.Input
{
    /// <summary>
    /// Represents a state of keystrokes recorded by a keyboard input device.
    /// </summary>
    public struct KeyboardState
    {
        /// <summary>
        /// List of pressed keys.
        /// </summary>
        private static List<Keys> pressedKeys = new List<Keys>();

        /// <summary>
        /// Sets the key state.
        /// </summary>
        /// <param name="key">Key to change.</param>
        /// <param name="state">New state for the key.</param>
        internal void SetKeyState(Keys key, KeyState state)
        {
            switch (state)
            {
                case KeyState.Up:
                    if (pressedKeys.Contains(key))
                    {
                        pressedKeys.Remove(key);
                    }
                    break;
                case KeyState.Down:
                    if (!pressedKeys.Contains(key))
                    {
                        pressedKeys.Add(key);
                    }
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Initializes a new instance of the KeyboardState class.
        /// </summary>
        /// <param name="keys">Array or parameter list of Keys to initialize as pressed.</param>
        public KeyboardState(params Keys[] keys)
        {
            pressedKeys = new List<Keys>(keys);
        }

        /// <summary>
        /// Compares two objects to determine whether they are different.
        /// </summary>
        /// <param name="a">Object to the left of the inequality operator.</param>
        /// <param name="b">Object to the right of the inequality operator.</param>
        /// <returns>true if the objects are different; false otherwise.</returns>
        public static bool operator !=(KeyboardState a, KeyboardState b)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Compares two objects to determine whether they are the same.
        /// </summary>
        /// <param name="a">Object to the left of the equality operator.</param>
        /// <param name="b">Object to the right of the equality operator.</param>
        /// <returns>true if the objects are the same; false otherwise.</returns>
        public static bool operator ==(KeyboardState a, KeyboardState b)
        {
            throw new NotImplementedException();
        }
    
        /// <summary>
        /// Returns the state of a particular key.
        /// </summary>
        /// <param name="key">Enumerated value representing the key to query.</param>
        /// <returns>The state of the key specified by key.</returns>
        public KeyState this[Keys key]
        {
            get
            {
                if (pressedKeys.Contains(key))
                {
                    return KeyState.Down;
                }
                else
                {
                    return KeyState.Up;
                }
            }
        }
 
        /// <summary>
        /// Returns a value that indicates whether the current instance is equal to a
        /// specified object.
        /// </summary>
        /// <param name="obj">Object to compare this object to.</param>
        /// <returns>true if the objects are the same; false otherwise.</returns>
        public override bool Equals(object obj)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the hash code for this instance.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets an array of values that correspond to the keyboard keys that are currently
        /// being pressed.
        /// </summary>
        /// <returns>The keys that are currently pressed.</returns>
        public Keys[] GetPressedKeys()
        {
            return pressedKeys.ToArray();
        }

        /// <summary>
        /// Returns whether a specified key is currently being pressed.
        /// </summary>
        /// <param name="key">Enumerated value that specifies the key to query.</param>
        /// <returns>true if the key specified by key is being held down; false otherwise.</returns>
        public bool IsKeyDown(Keys key)
        {
            if (pressedKeys.Contains(key))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Returns whether a specified key is currently not pressed.
        /// </summary>
        /// <param name="key">Enumerated value that specifies the key to query.</param>
        /// <returns>true if the key specified by key is not pressed; false otherwise.</returns>
        public bool IsKeyUp(Keys key)
        {
            return !this.IsKeyDown(key);
        }
    }
}
